﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using eShop.DataAccess.Entity;

namespace eShop.DataAccess
{
    public partial class AddressDAL : BaseDAL
    {
        public static bool Insert(Address address)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Address_InsertOrUpdate");
            db.AddOutParameter(dbCommand, "Id", DbType.Int32, 4);
            db.AddInParameter(dbCommand, "UserId", DbType.Int32, address.UserId);
            db.AddInParameter(dbCommand, "Name", DbType.String, address.Name);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, address.PhoneNumber);
            db.AddInParameter(dbCommand, "AddressLine1", DbType.String, address.AddressLine1);
            db.AddInParameter(dbCommand, "AddressLine2", DbType.String, address.AddressLine2);
            db.AddInParameter(dbCommand, "Zip", DbType.String, address.Zip);
            db.AddInParameter(dbCommand, "City", DbType.String, address.City);
            db.AddInParameter(dbCommand, "State", DbType.String, address.State);
            db.AddInParameter(dbCommand, "Country", DbType.String, address.Country);

            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                address.Id = Convert.ToInt32(db.GetParameterValue(dbCommand, "@Id"));
                return true;
            }
            return false;
        }

        public static bool Update(Address address)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Address_InsertOrUpdate");
            db.AddInParameter(dbCommand, "Id", DbType.Int32, address.Id);
            db.AddInParameter(dbCommand, "UserId", DbType.Int32, address.UserId);
            db.AddInParameter(dbCommand, "Name", DbType.String, address.Name);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, address.PhoneNumber);
            db.AddInParameter(dbCommand, "AddressLine1", DbType.String, address.AddressLine1);
            db.AddInParameter(dbCommand, "AddressLine2", DbType.String, address.AddressLine2);
            db.AddInParameter(dbCommand, "Zip", DbType.String, address.Zip);
            db.AddInParameter(dbCommand, "City", DbType.String, address.City);
            db.AddInParameter(dbCommand, "State", DbType.String, address.State);
            db.AddInParameter(dbCommand, "Country", DbType.String, address.Country);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        public static List<Address> GetAll(Boolean allowPaging = false, Int32 pageIndex = 0, Int32 pageSize = 0, String orderColumn = "Id", Boolean isAscending = false)
        {
            List<Address> addressList = new List<Address>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Address_GetAll");
            db.AddInParameter(dbCommand, "AllowPaging", DbType.Boolean, allowPaging);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbCommand, "OrderColumn", DbType.String, orderColumn);
            db.AddInParameter(dbCommand, "IsAscending", DbType.Boolean, isAscending);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                addressList = GetEntitiesFromReader(dataReader);
            }
            return addressList;
        }

        public static List<Address> GetByKeys(Int32? id, Int32? userId)
        {
            List<Address> addressList = new List<Address>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Address_GetByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (userId.HasValue)
                db.AddInParameter(dbCommand, "UserId", DbType.Int32, userId);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                addressList = GetEntitiesFromReader(dataReader);
            }
            return addressList;
        }

        public static bool DeleteByKeys(Int32? id, Int32? userId)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("Address_DeleteByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (userId.HasValue)
                db.AddInParameter(dbCommand, "UserId", DbType.Int32, userId);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        private static List<Address> GetEntitiesFromReader(IDataReader dataReader)
        {
            List<Address> addressList = new List<Address>();
            while (dataReader.Read())
            {
                addressList.Add(GetEntityFromReader(dataReader));
            }
            return addressList;
        }

        private static Address GetEntityFromReader(IDataReader dataReader)
        {
            Address address = new Address();
            address.Id = (Int32)dataReader["Id"];
            address.UserId = (Int32)dataReader["UserId"];
            address.Name = (String)dataReader["Name"];
            address.PhoneNumber = (String)dataReader["PhoneNumber"];
            address.AddressLine1 = (String)dataReader["AddressLine1"];
            address.AddressLine2 = (String)dataReader["AddressLine2"];
            address.Zip = (String)dataReader["Zip"];
            address.City = (String)dataReader["City"];
            address.State = (String)dataReader["State"];
            address.Country = (String)dataReader["Country"];
            return address;
        }
    }
}

